<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/7 0007
 * Time: 下午 13:30
 */

namespace Home\Controller;
use Think\Controller;

class UserController extends Controller{

    //当前userid的个人中心页面,客服和客户是不同的模板
    public function user(){
        $userid = cookie('userid');
        //$userid = 'WangJun';
        //$userid = 'qingchun';
        $this->assign(array(
            'title' => '个人中心',
            'page_title' => '我的',
            'userid' => $userid,
        ));
        $kefumodle = D('kefu');
        if($kefumodle->jc_user($userid)) {  //在客服表是客服
            $this->display('kefu');
        }else{
            $this->display('yonghu');
        }
    }

    //获取当前userid的详细信息(头像,名称,微信号)
    public function get_user_detail(){
        $userid = I('get.userid');
        $usermodel = D('Home/users');
        $info = $usermodel->getusersinfo($userid);
        $this->ajaxReturn($info);
    }


    //当前userid的聊天消息列表
    public function kefu_msg(){
        $userid = I('get.userid');  //当前userid
        $type = I('get.type');   //当前userid的类型  客服  or  客户
        $receiverid = I('get.receiver_id');  //此变量存在,表示是从推送的消息进入 谁推的消息

        //取出所有与当前userid聊天的用户
        $onlinemodel = D('online');
        $info = $onlinemodel->get_chat_jl($userid,$type);
        //dump($info);exit;

        if($receiverid){
            //判断此userid在users表有没有数据,没有就添加,设置cookie
            $usersmodel = D('Home/users');
            if(!$usersmodel->where("userid='{$userid}'")->find()){
                $access_token = componentsgetaccesstoken(); //获取access_token
                $usersmodel->setuser($access_token,$userid); //获取具体消息
            }
            cookie('userid',$userid); //设置cookie
            //输出到页面
            $this->assign('receiver_id',$receiverid);
        }

        $this->assign(array(
            'title' => '我的消息',
            'page_title' => '我的',
            'info' => $info,   //  当前userid与所有人的聊天数据
            'userid' => $userid, //  当前userid
            'type' => $type  //当前userid类型  客服 or 客户
        ));
        $this->display();
    }

    //客服聊天窗口
    public function kefu_chat(){
        $userid = I('get.userid');  //当前客服ID
        $receiverid = I('get.receiver_id');  //当前客户ID
        //dump($userid.'-----'.$receiverid);exit;

        /*******************在聊天状态表里更改当前客服和客户的聊天状态为在线状态,未读消息清零*******************/
        //更改状态
        $onlinemodel = D('online');
        $onlinemodel->update_online($receiverid,$userid,'kefu_type');
        //未读消息清零
        $onlinemodel->init_num($receiverid,$userid,'kefu_wd_num');

        //取出当前客户和客服的具体信息
        $usersmodel = D('Home/users');
        $userinfo = $usersmodel->getusersinfo($userid);  //当前客服信息
        $receiverinfo = $usersmodel->getusersinfo($receiverid);  //当前客户信息
        //dump($userinfo);//dump($receiverinfo);exit;

        /*************************获取当前客服和客户的聊天消息****************************/
        $receivermodel = D('receiver_msg');
        $arr = $receivermodel->get_msg($userid,$receiverid);
        //dump($arr);exit;

       //取出当前客服和客户消息的最大ID
        $sendmodel = D('send_msg');
        $rmaxid = $sendmodel->getmaxid($receiverid,$userid);


        $this->assign(array(
            'info' => $arr,  //消息数据
            'rmaxid' => $rmaxid,  //当前客服和客户消息的最大ID
            'userid' => $userid,   //当前客服ID
            'user_img' => $userinfo['avatar'],  //当前客服头像
            'receiverid' => $receiverid,    //当前客户ID
            'receiver_name' => $receiverinfo['name'], //当前客户名称
            'receiver_img' => $receiverinfo['avatar'] //当前客户头像
        ));
        $this->display();
    }


    //从数据库里取出最新消息
    public function ajax_getmsg(){
        $rmaxid = I('get.rmaxid');
        $userid = I('get.userid');
        $receiverid = I('get.receiverid');
        $sendmodel = D('send_msg');
        $rmaxid_new = $sendmodel->getmaxid($receiverid,$userid);
        $arr = false;
        if($rmaxid_new > $rmaxid){
            $receiver = D()->query("select * from send_msg where send_id='{$receiverid}' and receiver_id='{$userid}' and id>{$rmaxid} order by time asc");
            $arr = $receiver;
        }
        //dump($arr);exit;
        $this->ajaxReturn(array(
            'info' => $arr,
            'rmaxid' => $rmaxid_new,
        ));
    }


}